import { _decorator, Component, Node, resources, SpriteFrame, UITransform } from 'cc'
import { TileMap } from '../Tile/TileMap'
import { createUINode } from '../../Utils'
import levels, { ILevel } from '../../Levels'
import { DataCenter } from '../../Runtime/DataCenter'
import EventCenter from '../../Runtime/EventCenter'
import { EVENT_ENUM } from '../../Enum'
import ResourceCenter from '../../Runtime/ResourceCenter'
import { PlayerController } from '../Player/PlayerController'
import { flatten } from '../../Utils/helper'
import { WoodenSkullController } from '../Enemy/WoodenSkullController'
import { IronSkullController } from '../Enemy/IronSkullController'
import { TrapController } from '../Trap/TrapController'
import { DoorController } from '../Door/DoorController'
const { ccclass, property } = _decorator

@ccclass('BattleManager')
export class BattleManager extends Component {
  level: ILevel
  stage: Node

  protected onLoad(): void {
    EventCenter.on(EVENT_ENUM.NEXT_LEVEL, this.nextLevel, this)
  }
  protected onDestroy(): void {
    EventCenter.off(EVENT_ENUM.NEXT_LEVEL, this.nextLevel)
  }
  async start() {
    await this.loadResources()
    this.generateStage()
    this.initLevel()
  }
  //预加载所有资源
  async loadResources() {
    const allTextureSpriteFrame = await Promise.all([
      //玩家
      ResourceCenter.loadSpriteDir('texture/player/idle', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/attack', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/turnleft', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/turnright', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/airdeath', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/death', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/blockback', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/blockfront', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/blockleft', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/blockright', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/blockturnleft', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/player/blockturnright', SpriteFrame),

      //木骷髅
      ResourceCenter.loadSpriteDir('texture/woodenskeleton/idle', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/woodenskeleton/attack', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/woodenskeleton/death', SpriteFrame),

      //铁骷髅
      ResourceCenter.loadSpriteDir('texture/ironskeleton/idle', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/ironskeleton/attack', SpriteFrame),
      // ResourceCenter.loadSpriteDir('texture/ironskeleton/death', SpriteFrame),

      //陷阱
      ResourceCenter.loadSpriteDir('texture/spikes/spikesfour', SpriteFrame),

      //人物路径烟雾
      ResourceCenter.loadSpriteDir('texture/smoke', SpriteFrame),

      //门
      ResourceCenter.loadSpriteDir('texture/door', SpriteFrame),
    ])
    DataCenter.allSpriteFrame = flatten(allTextureSpriteFrame)
  }
  initLevel() {
    const { levelIndex } = DataCenter
    const level = levels[`level${levelIndex}`]
    if (level) {
      this.clearLevel()
      this.level = level
      const rowCount = level.mapInfo.length
      const colCount = level.mapInfo[0].length
      DataCenter.rowCount = rowCount
      DataCenter.colCount = colCount
      DataCenter.mapInfo = level.mapInfo

      //生成瓦片地图
      this.generateTileMap()

      //生成玩家
      this.generatePlayer()

      //生成敌人
      this.generateEnemy()

      //生成陷阱
      this.generateTrap()

      //生成门
      this.generateDoor()
    }
  }
  generateStage() {
    const stage = createUINode('stage')
    this.stage = stage
    stage.parent = this.node
  }

  generateTileMap() {
    const tileMap = createUINode('tileMap')
    tileMap.parent = this.stage
    const tileMapScript = tileMap.addComponent(TileMap)
    tileMapScript.init()

    this.adaptPos()
  }

  adaptPos() {
    const { TILE_HEIGHT, TILE_WIDTH, rowCount, colCount } = DataCenter
    const posX = (TILE_WIDTH * rowCount) / 2
    const posY = (TILE_HEIGHT * colCount) / 2 + 80
    this.stage.setPosition(-posX, posY)
  }

  // 生成玩家
  generatePlayer() {
    const player = createUINode('player')
    player.parent = this.stage
    const playerController = player.addComponent(PlayerController)
    playerController.init()
  }

  //生成敌人
  generateEnemy() {
    const enemy = createUINode('enemy')
    enemy.parent = this.stage
    // const enemyController = enemy.addComponent(WoodenSkullController)
    const enemyController = enemy.addComponent(IronSkullController)
    enemyController.init()
  }

  //生成陷阱
  generateTrap() {
    const trap = createUINode('trap')
    trap.parent = this.stage
    const trapController = trap.addComponent(TrapController)
    trapController.init()
  }

  //生成门
  generateDoor() {
    const door = createUINode('door')
    door.parent = this.stage
    const doorController = door.addComponent(DoorController)
    doorController.init()
  }

  //下一关
  nextLevel() {
    const { levelIndex } = DataCenter
    const nextLevelIndex = levelIndex + 1
    DataCenter.levelIndex = nextLevelIndex
    this.initLevel()
  }
  //清空关卡
  clearLevel() {
    this.stage.removeAllChildren()
    DataCenter.reset()
  }
}
